/*
 * Created on Feb 5, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package weather.ui.systray;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import weather.net.Proxy;

/**
 * @author Brian
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class PreferenceForm {
    private Display display;

    private Proxy proxy;

    private Shell shell;

    private Text txtHost;

    private Text txtPort;

    Button btn;

    public PreferenceForm(Display display) {
        this.display = display;
        proxy = new Proxy();

        // Create the window
        shell = new Shell(display);
        shell.setBounds(400, 400, 200, 200);
        shell.setText("Configure Proxy Server");

        Label lblTitle = new Label(shell, SWT.HORIZONTAL);
        lblTitle.setBounds(30, 10, 200, 20);
        lblTitle.setText("Configure Proxy Settings");
        lblTitle.setForeground(new Color(display, 0, 0, 255));

        Label lblHost = new Label(shell, SWT.HORIZONTAL);
        lblHost.setBounds(1, 30, 39, 20);
        lblHost.setText("Host: ");

        Label lblPort = new Label(shell, SWT.HORIZONTAL);
        lblPort.setBounds(1, 50, 39, 20);
        lblPort.setText("Port: ");

        // Add a new textbox to the shell
        txtHost = new Text(shell, SWT.BORDER);
        txtHost.setBounds(40, 30, 100, 20);
        if (proxy.getHost() != null && !proxy.getHost().equals("no proxy")) {
            txtHost.setText(proxy.getHost());
        }

        txtPort = new Text(shell, SWT.BORDER);
        txtPort.setBounds(40, 50, 100, 20);
        if (proxy.getPort() != null) {
            txtPort.setText(proxy.getPort());
        }

        // Add a new button to the shell
        btn = new Button(shell, SWT.PUSH);
        btn.setBounds(30, 80, 100, 20);

        // Set the button's caption
        btn.setText("Apply Configuration");

        btn.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                String host = null;
                String port = null;
                if (txtHost.getText() != null) {
                    host = txtHost.getText();
                }
                if (txtPort.getText() != null) {
                    port = txtPort.getText();
                }
                if (host != null && port != null) {
                    proxy.configureProxy(host, port);
                }
                shell.dispose();
            }
        });

    }

    public void show() {
        shell.open();

    }

}
